using System;
using System.Data;
using System.Data.Common;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using log4net;
using log4net.Config;

namespace ProjectBuilder
{
    public class Builder
    {
        public static int Main()
        {
            var connStr = "";
            try
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
                connStr = ConfigurationManager.AppSettings["mysqlConnectionString"].ToString();
            }
            catch (Exception ex)
            {
                LogManager.GetLogger("Main").Error("Read config", ex);
                return 1;
            }
            var start = DateTime.Now;
            LogManager.GetLogger("Main").Info("Start application");

            LogManager.GetLogger("Main").Info("Connecting to MySQL...");
            Globals.Connection = new MySqlConnection(connStr);
            Globals.Connection.Open();
                        
            try
            {
                Globals.Project = new Project("bazalt", ConfigurationManager.AppSettings["basePath"].ToString());
                LogManager.GetLogger("Main").Info("Update project sources...");
                Globals.Project.parseUpdateResults();
                Globals.Project.build();
                
                LogManager.GetLogger("Main").Info("Done: " + DateTime.Now.Subtract(start).TotalSeconds + " sec");
            }
            catch (Exception ex)
            {
                LogManager.GetLogger("Main").Error("Application run", ex);
                Console.ReadLine();
                return 1;
            }
            finally
            {
                Globals.Connection.Close();
            }
            return 0;
        }
    }
}